package com.group4.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;


/**
 * @author fengbenpeng
 * @description: 这里是配置文件
 * @date 2022/3/12 15:49
 */
public class RabbitMQConfig {
    /**
     * 添加订单,交换机,队列,绑定
     */
    //交换机名称,自己取名字
    public static final String ORDER_TOPIC_EXCHANGE = "order_topic_exchange";
    //队列名称,自己取名字
    public static final String ORDER_ADD_QUEUE = "order_add_email_queue";
    //取消队列
    public static final String ORDER_CANCEL_QUEUE = "order_cancel_email_queue";
    //ttl队列
    public static final String TTl_QUEUE = "ttl_queue";
    //死信队列
    public static final String DEAD_QUEUE = "dead_queue";


    //声明交换机,Bean后面如果不写@Bean("orderTopicExchange"),默认的bean就是方法名topicExchange
    @Bean("orderTopicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(ORDER_TOPIC_EXCHANGE).durable(true).build();
    }

    //声明队列 ,Bean后面如果不写@Bean("orderAddEmailQueue"),默认的bean就是方法名itemQueue
    @Bean("orderAddEmailQueue")
    public Queue itemQueue(){
        return QueueBuilder.durable(ORDER_ADD_QUEUE).build();
    }

    //声明ttl
    @Bean("ttlQueue")
    public Queue ttlQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 60000);  //设置ttl（单位是毫秒，表示存活时间，超过时间则失效）
        //表示60秒以后,ttl里面的消息会自动跳转到死信交换机ORDER_TOPIC_EXCHANGE里面
        args.put("x-dead-letter-exchange", ORDER_TOPIC_EXCHANGE);//消息超过存活时间则发往该交换机,就是死信交换机
        args.put("x-dead-letter-routing-key", "dead.msg"); //按照该路由键将消息路由到队列
//        return new Queue("laotan-order-queue", true, false, false, args);

        return QueueBuilder.durable(TTl_QUEUE).withArguments(args).build();
    }
    //绑定ttl队列
    @Bean("cancelOrderQueueExchange")
    public Binding cancelOrderQueueExchange(@Qualifier("ttlQueue") Queue queue,
                                            @Qualifier("orderTopicExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl.#").noargs();
    }

    //声明死信队列
    @Bean("deadQueue")
    public Queue deadQueue() {
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }
    //绑定死信队列与交换机
    @Bean("deadQueueExchange")
    public Binding deadQueueExchange(@Qualifier("deadQueue") Queue queue,
                                     @Qualifier("orderTopicExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dead.#").noargs();
    }
    //声明取消订单队列
    @Bean("orderCancelEmailQueue")
    public Queue cancelQueue() {

        return QueueBuilder.durable(ORDER_CANCEL_QUEUE).build();
    }
    //绑定取消队列与交换机
    @Bean("cancelQueueExchange")
    public Binding cancelQueueExchange(@Qualifier("orderCancelEmailQueue") Queue queue,
                                       @Qualifier("orderTopicExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("cancel.email.#").noargs();
    }


}
